package com.hb.znpt.model;

import com.hb.znpt.BuildConfig;
import com.hb.znpt.bean.LandBean;
import com.hb.znpt.bean.LesseeBean;
import com.hb.znpt.utils.AESHttpUtil;
import com.hb.znpt.utils.Constants;
import com.hb.znpt.utils.LogUtils;
import com.unitlib.base.base.BaseModel;
import com.unitlib.base.utils.SPUtils;

import org.litepal.LitePal;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import io.reactivex.Observable;
import rxhttp.wrapper.param.RxHttp;

public class LandModel extends BaseModel {

    Calendar calendar = Calendar.getInstance();

    public Observable<List<LandBean>> getList(String deptid) {
        int cunYear = calendar.get(Calendar.YEAR);
        long t = new Date().getTime();
        String json = "{\"deptid\":\"" + deptid +
                "\",\"year\":\"" + cunYear +
                "\",\"sfz\":\"" + SPUtils.getInstance().getString(Constants.SP_USERCARD) +
                "\",\"token\":\"" + SPUtils.getInstance().getString(Constants.SP_TOKEN) +
                "\",\"bb\":\"" + BuildConfig.VERSION_CODE +
                "\",\"timestamp\":" + t + "}";
        LogUtils.e(TAG, json);
        String encrypt = null;
        try {
            encrypt = AESHttpUtil.加密(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RxHttp.postJson(Constants.URL_YSC)
                .add("encrypt", encrypt)
                .add("timestamp", t)
                .asResponseList(LandBean.class);
    }

    public List<LandBean> getOffData(String deptid) {
        return LitePal.where("deptid = ?", deptid).find(LandBean.class);
    }

    public int getOffDataCount(String deptid) {
        return LitePal.where("deptid = ?", deptid).count(LandBean.class);
    }

}
